package GUI;

import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.border.BevelBorder;
import javax.swing.JLabel;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.ArrayList;

import javax.swing.SwingConstants;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.border.EtchedBorder;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.JPasswordField;
import javax.swing.JComboBox;
import javax.swing.JButton;
import javax.swing.DefaultComboBoxModel;

import Database.Database;
import Database.DatabaseExtended;

import GUI.MainScreen;

public class LoginScreen extends JFrame implements KeyListener {

	private JPanel contentPane;
	private JTextField usertextField;
	private JPasswordField passPasswordField;
	private JButton btnLogin;
	private JButton btnClear;
	private JButton btnExit;

	JFrame thisFrame = this;

	/**
	 * Create the frame.
	 */
	public LoginScreen() {
		setVisible(true);
		setTitle("Login");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setSize(302, 267);
		setLocationRelativeTo(null);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		setResizable(false);

		JPanel panel = new JPanel();
		panel.setBorder(new BevelBorder(BevelBorder.RAISED, null, null, null,
				null));

		JPanel panel_1 = new JPanel();
		panel_1.setBorder(new EtchedBorder(EtchedBorder.LOWERED, null, null));

		// Login Button
		btnLogin = new JButton("Login");
		btnLogin.addActionListener(new ActionListener() {
			/**
			 * Set the login details
			 * 
			 * @param Username
			 *            the name of the user (String)
			 * @param Password
			 *            the word to access the system (String)
			 * 
			 *            Initialise and connect the database
			 * 
			 *            Check User Credentials
			 * @param result
			 *            Check to see if the user name and password are correct
			 *            (boolean)
			 */
			public void actionPerformed(ActionEvent arg0) {

				String UserName = usertextField.getText();
				String UserPassword = passPasswordField.getText();

				// pass the parameters to connect with the database
				Database.initialise("root", "", "localhost/programming2");

				// use method that compares user name and password in the user
				// table
				// of the database
				boolean result = DatabaseExtended.checkUserCredentials(
						UserName, UserPassword);

				// If statement to see if the user name and password are
				// correct. Show Message Dialog if incorrect
				if (result) {
					new MainScreen();
					thisFrame.dispose();
				} else {
					JOptionPane.showMessageDialog(thisFrame,
							"Incorrect Login Details");
				}

			}
		});

		// Clear Button
		btnClear = new JButton("Clear");
		btnClear.addActionListener(new ActionListener() {
			/**
			 * Clear the text-fields
			 */
			public void actionPerformed(ActionEvent e) {
				usertextField.setText("");
				passPasswordField.setText("");
			}
		});
		// Exit Button
		btnExit = new JButton("Exit");
		btnExit.addActionListener(new ActionListener() {
			/**
			 * Dispose the screen
			 */
			public void actionPerformed(ActionEvent arg0) {
				dispose();
			}
		});

		GroupLayout gl_contentPane = new GroupLayout(contentPane);
		gl_contentPane
				.setHorizontalGroup(gl_contentPane
						.createParallelGroup(Alignment.LEADING)
						.addGroup(
								gl_contentPane
										.createSequentialGroup()
										.addContainerGap()
										.addGroup(
												gl_contentPane
														.createParallelGroup(
																Alignment.LEADING)
														.addGroup(
																gl_contentPane
																		.createSequentialGroup()
																		.addGap(11)
																		.addComponent(
																				btnLogin)
																		.addGap(31)
																		.addComponent(
																				btnClear)
																		.addPreferredGap(
																				ComponentPlacement.RELATED,
																				36,
																				Short.MAX_VALUE)
																		.addComponent(
																				btnExit)
																		.addGap(23))
														.addGroup(
																gl_contentPane
																		.createSequentialGroup()
																		.addGroup(
																				gl_contentPane
																						.createParallelGroup(
																								Alignment.TRAILING)
																						.addComponent(
																								panel_1,
																								Alignment.LEADING,
																								GroupLayout.DEFAULT_SIZE,
																								256,
																								Short.MAX_VALUE)
																						.addComponent(
																								panel,
																								GroupLayout.DEFAULT_SIZE,
																								256,
																								Short.MAX_VALUE))
																		.addContainerGap()))));
		gl_contentPane.setVerticalGroup(gl_contentPane.createParallelGroup(
				Alignment.LEADING)
				.addGroup(
						gl_contentPane
								.createSequentialGroup()
								.addContainerGap()
								.addComponent(panel,
										GroupLayout.PREFERRED_SIZE, 61,
										GroupLayout.PREFERRED_SIZE)
								.addPreferredGap(ComponentPlacement.UNRELATED)
								.addComponent(panel_1,
										GroupLayout.PREFERRED_SIZE, 84,
										GroupLayout.PREFERRED_SIZE)
								.addGap(18)
								.addGroup(
										gl_contentPane
												.createParallelGroup(
														Alignment.BASELINE)
												.addComponent(btnLogin)
												.addComponent(btnClear)
												.addComponent(btnExit))
								.addContainerGap()));

		JLabel lblUsername = new JLabel("Username:");

		JLabel lblPassword = new JLabel("Password:");

		usertextField = new JTextField("ruth");
		usertextField.setColumns(10);

		passPasswordField = new JPasswordField("password");
		GroupLayout gl_panel_1 = new GroupLayout(panel_1);
		gl_panel_1.setHorizontalGroup(gl_panel_1.createParallelGroup(
				Alignment.LEADING).addGroup(
				gl_panel_1
						.createSequentialGroup()
						.addGap(22)
						.addGroup(
								gl_panel_1
										.createParallelGroup(Alignment.LEADING)
										.addComponent(lblUsername)
										.addComponent(lblPassword))
						.addPreferredGap(ComponentPlacement.RELATED, 26,
								Short.MAX_VALUE)
						.addGroup(
								gl_panel_1
										.createParallelGroup(Alignment.LEADING,
												false)
										.addComponent(passPasswordField)
										.addComponent(usertextField,
												GroupLayout.DEFAULT_SIZE, 131,
												Short.MAX_VALUE)).addGap(21)));
		gl_panel_1
				.setVerticalGroup(gl_panel_1
						.createParallelGroup(Alignment.LEADING)
						.addGroup(
								gl_panel_1
										.createSequentialGroup()
										.addContainerGap()
										.addGroup(
												gl_panel_1
														.createParallelGroup(
																Alignment.BASELINE)
														.addComponent(
																usertextField,
																GroupLayout.PREFERRED_SIZE,
																GroupLayout.DEFAULT_SIZE,
																GroupLayout.PREFERRED_SIZE)
														.addComponent(
																lblUsername))
										.addGap(18)
										.addGroup(
												gl_panel_1
														.createParallelGroup(
																Alignment.BASELINE)
														.addComponent(
																lblPassword)
														.addComponent(
																passPasswordField,
																GroupLayout.PREFERRED_SIZE,
																GroupLayout.DEFAULT_SIZE,
																GroupLayout.PREFERRED_SIZE))
										.addContainerGap(50, Short.MAX_VALUE)));
		panel_1.setLayout(gl_panel_1);

		JLabel lblLogin = new JLabel("Login");
		lblLogin.setHorizontalAlignment(SwingConstants.CENTER);
		lblLogin.setFont(new Font("Arial", Font.BOLD, 18));
		GroupLayout gl_panel = new GroupLayout(panel);
		gl_panel.setHorizontalGroup(gl_panel.createParallelGroup(
				Alignment.LEADING).addGroup(
				gl_panel.createSequentialGroup()
						.addContainerGap()
						.addComponent(lblLogin, GroupLayout.DEFAULT_SIZE, 232,
								Short.MAX_VALUE).addContainerGap()));
		gl_panel.setVerticalGroup(gl_panel.createParallelGroup(
				Alignment.LEADING).addGroup(
				gl_panel.createSequentialGroup()
						.addContainerGap()
						.addComponent(lblLogin, GroupLayout.DEFAULT_SIZE, 35,
								Short.MAX_VALUE).addContainerGap()));
		panel.setLayout(gl_panel);
		contentPane.setLayout(gl_contentPane);
	}

	@Override
	public void keyPressed(KeyEvent k) {
		if (k.getKeyCode() == KeyEvent.VK_ENTER) {

			for (ActionListener a : btnLogin.getActionListeners())
				a.actionPerformed(new ActionEvent(this,
						ActionEvent.ACTION_PERFORMED, null));
		}

	}

	@Override
	public void keyReleased(KeyEvent arg0) {
		// TODO Auto-generated method stub

	}

	@Override
	public void keyTyped(KeyEvent arg0) {
		// TODO Auto-generated method stub

	}
}
